package com.zs.grain.ui

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.binioter.guideview.Component
import com.binioter.guideview.GuideBuilder
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qw.curtain.lib.Curtain
import com.qw.curtain.lib.ViewGetter
import com.xnkj1688.semplibrary.mvvm.BaseMvvmFragment
import com.xnkj1688.semplibrary.recyclerview.ClickHandler
import com.xnkj1688.semplibrary.recyclerview.ItemView
import com.zs.grain.ARouterPath
import com.zs.grain.BR
import com.zs.grain.R
import com.zs.grain.databinding.FragmentMenusBinding
import com.zs.grain.tool.LottieComponent
import com.zs.grain.tool.MutiComponent
import com.zs.grain.tool.SimpleComponent
import io.reactivex.annotations.NonNull
import kotlinx.android.synthetic.main.fragment_menus.*


@Route(path = ARouterPath.MenusFragment)
class MenusFragment : BaseMvvmFragment<FragmentMenusBinding, MenusViewModel>() {
    override val layoutId: Int = R.layout.fragment_menus

//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//
//
//    }

    override fun initViewModel(binding: FragmentMenusBinding) {
        binding.vm = this.mViewModel
        binding.view = this

        LiveEventBus.get("RefreshMenu", Boolean::class.java)
            .observe(this, Observer {
                mViewModel.refreshComponentStatus()
            })

        LiveEventBus.get("RefreshRV", Bundle::class.java)
            .observe(this, Observer {
                val x = it.getInt("x")
                val y = it.getInt("y")
                binding.rvMenus.scrollBy(x,y)
            })

        //binding.rvMenus
        binding.rvMenus.addOnScrollListener(
            object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(
                    @NonNull recyclerView: RecyclerView,
                    newState: Int
                ) {
                    super.onScrollStateChanged(recyclerView, newState)
                }

                override fun onScrolled(
                    @NonNull recyclerView: RecyclerView,
                    dx: Int,
                    dy: Int
                ) {
                    super.onScrolled(recyclerView, dx, dy)
                    if(!binding.rvMenus.canScrollVertically(1))
                        mViewModel.nextMore.postValue(false)
//                    val offset = binding.rvMenus.computeVerticalScrollOffset()
//                    val range = binding.rvMenus.computeVerticalScrollRange()-binding.rvMenus.computeVerticalScrollExtent()
//                    if(range==0) mViewModel.nextMore.postValue(false)
//                    if(!(offset<range-1)) mViewModel.nextMore.postValue(false)
//                    Log.i("--offset:",offset.toString())
//                    Log.i("range:",range.toString())
                    //var y = computeHorizontalScrollOffset()
                    //binding.rvMenus.canKeep
                    if(!binding.rvMenus.canScrollVertically(-1))
                        mViewModel.preMore.postValue(false)
                    if(binding.rvMenus.canScrollVertically(-1)&&binding.rvMenus.canScrollVertically(1)){
                        mViewModel.preMore.postValue(true)
                        mViewModel.nextMore.postValue(true)
                    }
                }
            }
        )

    }

    override fun onResume() {
        super.onResume()

        mViewModel.init(1)
    }
//    override fun initAction() {
//        mViewModel.init(1)
//    }

    val menuItemView = ItemView<MenuViewModel>(BR.vm, R.layout.item_menu)

    val menuClickHandler = ClickHandler { vm: MenuViewModel ->
        mViewModel.onClickMenu(vm)
    }

    fun showGuideView() {
        val item1: View? = ViewGetter.getFromRecyclerView(this.rvMenus, 0)
        //val item2: View? = ViewGetter.getFromRecyclerView(recyclerView, 5)
        if (null == item1 ) {
            return
        }
        val builder = GuideBuilder()
        builder.setTargetView(item1)//this.btnMenu1
            .setAlpha(150)
            .setHighTargetCorner(20)
            .setHighTargetPadding(10)
        builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
            override fun onShown() {}
            override fun onDismiss() {
                showGuideView3()
            }
        })
        builder.addComponent(SimpleComponent())
        val guide = builder.createGuide()
        guide.show(this.activity)//@SimpleGuideViewActivity
    }

    fun showGuideView2() {
        val builder1 = GuideBuilder()
        builder1.setTargetView(this.btnMenu2)
            .setAlpha(150)
            .setHighTargetGraphStyle(Component.CIRCLE)
        builder1.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
            override fun onShown() {}
            override fun onDismiss() {
                //showGuideView3()
            }
        })
        builder1.addComponent(MutiComponent())
        val guide = builder1.createGuide()
        guide.show(this.activity)//@SimpleGuideViewActivity
    }

    fun showGuideView3() {
        val builder1 = GuideBuilder()
        builder1.setTargetView(this.rvMenus)
            .setAlpha(150)
            .setHighTargetCorner(20)
            .setHighTargetPadding(10)
            //.setExitAnimationId(android.R.anim.fade_out)
        builder1.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
            override fun onShown() {}
            override fun onDismiss() {}
        })
        builder1.addComponent(LottieComponent())
        val guide = builder1.createGuide()
        guide.setShouldCheckLocInWindow(false)
        guide.show(this.activity)//@SimpleGuideViewActivity
    }

    private fun showGuideInItemChild() {
        val item1: View? = ViewGetter.getFromRecyclerView(this.rvMenus, 0)
        //val item2: View? = ViewGetter.getFromRecyclerView(recyclerView, 5)
        if (null == item1 ) {
            return
        }
        Curtain(this)
            //.withShape(item1.findViewById(R.id.image), CircleShape())
            //.with(item2.findViewById(R.id.tv_text))
            //.show()
//        Curtain(this)
            .with(item1)
            .show()
    }

    override fun onUiNotify(name: String, bundle: Bundle) {
        super.onUiNotify(name, bundle)
        when (name) {
            "SwitchUI" -> {
                var menuNo = bundle.getLong("MenuNo")
                var activity = this.activity as MainActivity
            }
            "HideButton" ->{
                this.takeProBut.setVisibility(View.GONE)
            }
            "showGuideView"->{
                //this.showGuideInItemChild()
                this.showGuideView()
            }
        }
    }
}
